//
//  ADJgSDKBannerAdContainer.m
//  AnyThinkSDKDemo
//
//  Created by 刘娟 on 2024/9/26.
//  Copyright © 2024 抽筋的灯. All rights reserved.
//

#import "ADJgSDKBannerAdContainer.h"

@interface ADJgSDKBannerAdContainer()<ADJgSDKBannerAdViewDelegate>

@property(nonatomic, assign) CGFloat bidPrice;

@end

@implementation ADJgSDKBannerAdContainer
-(void)setDelegate:(id<ADJgSDKBannerAdViewDelegate>)delegate{
    self.bannerView.delegate = delegate;
}
// MARK: - Init
-(instancetype)initPosId:(NSString *)posId delegate:(id<ADJgSDKBannerAdViewDelegate>)delegate frame:(CGRect)frame{
    self = [super init];
    if(self){
        dispatch_async(dispatch_get_main_queue(), ^{
            self.bannerView = [[ADJgSDKBannerAdView alloc] initWithFrame:frame];
            self.bannerView.posId = posId;
            self.bannerView.delegate = delegate;
            if (delegate == nil) {
                self.bannerView.delegate = self;
            }
            [self loadAndShow];
        });
       
    }
    return self;
}

- (void)loadAndShow{
    if (self.bannerView) {
        [self.bannerView loadAndShow:self.controller];
    }
}

-(void)close{
    _bannerView = nil;
}

- (NSInteger)getBidPrice{
    return _bidPrice;
}

#pragma mark -- ADJgSDKBannerAdViewDelegate
/**
 *  开屏广告素材加载成功
 */
- (void)adjg_bannerViewDidReceived:(ADJgSDKBannerAdView *)bannerView {
#if DEBUG
        NSLog(@"ADJgSDK - 横幅 - 加载成功");
#endif
    ADJgSDKExtInfo *extInfo = [bannerView adjg_extInfo];
    _bidPrice = extInfo.ecpm.floatValue;
    if ([self.containerDelegate respondsToSelector:@selector(jgsdkContainer_jgsdkBannerAdDidLoad:withPrice:)]) {
        [self.containerDelegate jgsdkContainer_jgsdkBannerAdDidLoad:self withPrice:_bidPrice];
    }
    [self loadAndShow];
}

/**
 *  开屏广告请求失败
 */
- (void)adjg_bannerViewFailToReceived:(ADJgSDKBannerAdView *)bannerView errorModel:(ADJgAdapterErrorDefine *)errorModel {
    if ([self.containerDelegate respondsToSelector:@selector(jgsdkContainer_jgsdkBannerAdFailLoadError:)]) {
        [self.containerDelegate jgsdkContainer_jgsdkBannerAdFailLoadError:nil];
    }
}


@end
